import { CsvManager_blxf } from "../Manager/CsvManager";
import { EnumManager_blxf } from "../Manager/EnumManager";
import { ConfigModule_blxf } from "../Module/ConfigModule";
import { NumModule_blxf } from "../Module/NumModule";
import PublicModule_blxf from "../Module/PublicModule";
import { UtilsModule_blxf } from "../Module/UtilsModule";
import GameData_blxf from "./GameData";
import BaseShopData_blxf from "./ShopData";
import { VendorData_blxf } from "./VendorData";


/** 基础篮子数据 */
export class BaseTargetData_blxf {

    public constructor ( data: object ) {
        this.isUnlock  = data['isUnlock'];
        this.targetId  = data['TargetId'];
        this.targetType = data['TargetType'];
        this.buyShopId = data['buyShopId'] || -1;
        this.isRandomShopId = data['isRandomShopId'];

        this.unlockNum = Number(data['UnlockNum']);
        this.unlockType = Number(data['UnlockType']);

        this.buyShopNum = data['buyShopNum'] || 0;

        this.isAuto = data['isAuto'] || false;
        this.autoTargetData = data['autoTargetData'];

        this.initBuyShopInfo_blxf();

        this.initPassShopInfo_blxf();

        this.initTargetLvUpData_blxf();

        this.onCheckBuyShopNum_blxf();
    }

    /** 摊位解锁类型 */
    unlockType : number = 0;
    /** 摊位解锁数量 */
    unlockNum  : number = 0;


    /** 篮子id */
    targetId  : number = -1;
    /** 篮子类型 1：临时篮子、2：普通篮子、3：需求商品篮子 */
    targetType : number = -1;
    /** 篮子商品id */
    buyShopId : number = -1;
    /** 篮子是否解锁 */
    isUnlock  : boolean = false;

    /** 需求商品信息 */
    buyShopInfo : BaseShopData_blxf = null;
    /** 过关商品信息 */
    passShopInfo : any = null;
    /** 菜篮子当级数据 */
    targetLvData: any = null; 
    /** 菜篮子升级数据 */
    targetLvUpData : any = null;

    /** 最大商品数 */
    maxShopNum : number = 10;
    /** 商品基数 */
    baseShopNum : number = 1;
    /** 已经购买商品数 */
    buyShopNum : number = 0;

    /** 是否随便来点 */
    isRandomShopId : boolean = true;

    /** 是否自动售卖 */
    isAuto : boolean = false;
    /** 自动售卖菜篮数据 */
    autoTargetData : any = null;

    targetLv : number = 1;

    /** 清除摊位数据 */
    clearTarget_blxf () {
        this.buyShopNum = 0;

        if ( this.targetType == 1 ) {
            this.buyShopId = -1;
            this.isUnlock = false;
        }

        this.saveTargetData_blxf();
    }


    /** 解锁篮子 */
    onUnlockTarget_blxf () {
        this.isUnlock = true;
    }

    /** 检测篮子是否已解锁 */
    onCheckUnlockTarget_blxf () {
        return this.isUnlock;
    }

    onCheckBuyTarget_blxf () {

    }

    /** 获取菜篮子id */
    getTargetId_blxf () {
        return this.targetId;
    }

    /** 获取篮子类型 */
    getTargetType_blxf () {
        return this.targetType;
    }

    getTargetLv_blxf () {
        return this.targetLv;
        let { TargetLv } = this.targetLvData;
        return Number(TargetLv);
    }

    /** 获取菜篮子商品id */
    getBuyShopId_blxf () {
        let _isVideoTarget: boolean = this.targetType == 1;
        return !_isVideoTarget ? this.buyShopId : -1;
    }

    /** 获取已购买的商品数 */
    getBuyShopNum_blxf () {
        return this.buyShopNum;
    }

    /** 获取最大商品数 */
    getMaxShopNum_blxf () {
        return this.maxShopNum;
    }

    /** 获取需要的商品数 */
    getNeedShopNum_blxf () {
        return this.maxShopNum - this.buyShopNum;
    }

    /** 获取商品数进度 */
    getShopNumStr_blxf ( shopNum: number ) {
        return `${shopNum ? shopNum : this.buyShopNum}/${this.maxShopNum}`;
    }

    /** 是否是关卡相关菜篮子 */
    isPassTarget_blxf () {
        return this.isUnlock && this.targetType != 1;
        return this.targetType == 3;
    }

    /** 解锁菜篮子 */
    unlockTarget_blxf () {
        this.isUnlock = true;

        this.initPassShopInfo_blxf();

        this.initTargetLvUpData_blxf();

        this.targetType == 2 && this.initNormalTargetShopId_blxf();
    }

    /** 初始化普通菜篮子目标商品id */
    initNormalTargetShopId_blxf () {
        let _shopId: number = VendorData_blxf.instance.getVendorMaxShopId_blxf( this.buyShopId );
        this.onUpdateShopId_blxf( _shopId );
    }

    /** 重置临时菜篮子 */
    restartVideoTarget_blxf () {
        // this.isUnlock = false;
    }

    /** 检测是否可以解锁 */
    onCheckUnlock_blxf ( isBtn: boolean = false ) {
        let _isNum = NumModule_blxf.instance.onCheckNum_blxf( this.unlockType, this.unlockNum, isBtn );
        return _isNum;
    }

    /**
     * 检测篮子商品id
     * @param checkShopId 检测商品id
     * @returns 是否相同商品
     */
    onCheckShopId_blxf ( checkShopId: number ) {
        let _isBuy: boolean;

        if ( !checkShopId ) {
            UtilsModule_blxf.instance.Log_blxf('onCheckShopId_blxf 参数错误', 2);
            return _isBuy;
        }

        let _isVideoTarget: boolean = this.targetType == 1;

        _isBuy = _isVideoTarget || this.buyShopId == checkShopId;
        return _isBuy;
    }

    /** 检测是否初始化普通菜篮子 */
    onCheckInitNormalTarget_blxf () {
        let _isInitBuyShopId: boolean = this.targetType != 3 && this.buyShopId == -1;
        return _isInitBuyShopId;
    }

    /**
     * 检测商品数是否可以全部放入
     * @param checkShopNum 检测商品数
     * @returns 是否可以全部放入
     */
    onCheckNeedShopNum_blxf ( checkShopNum: number ) {
        let _isCheckNum: boolean;
        if ( !this.isUnlock ) return _isCheckNum;

        if ( !checkShopNum || checkShopNum < 0 ) {
            UtilsModule_blxf.instance.Log_blxf('onCheckNeedShopNum_blxf 参数错误', 2);
            return _isCheckNum;
        }

        let _needShopNum: number = this.getNeedShopNum_blxf();
        _isCheckNum = _needShopNum >= checkShopNum * this.baseShopNum;

        return _isCheckNum;
    }

    /** 检测是否购买商品完成 */
    onCheckBuyShopOk_blxf () {
        return this.buyShopNum >= this.maxShopNum;
    }
    
    /**
     * 放入商品
     * @param addShopNum 放入商品数
     */
    addBuyShopNum_blxf ( addShopNum: number ) {
        let _isAll: boolean;
        if ( !addShopNum || addShopNum < 1 ) {
            UtilsModule_blxf.instance.Log_blxf('addBuyShopNum_blxf 参数错误', 2);
            return _isAll
        }

        let _addNum: number = addShopNum * this.baseShopNum;
        this.buyShopNum += _addNum;

        // 检测篮子是否已满
        _isAll = this.onCheckBuyShopOk_blxf();
        // cc.log('_isAll >>', _isAll, this.buyShopNum, this.maxShopNum);
        if ( !_isAll ) {
            // 保存数据
            this.saveTargetData_blxf();

            return _isAll;
        }

        // 更新关卡进度
        this.addPassShopNum_blxf( Number(this.maxShopNum) );

        // 临时菜篮子(视频)
        let _isVideoTarget: boolean = this.targetType == 1;
        _isVideoTarget && this.restartVideoTarget_blxf();

        let _isShopTarget: boolean = this.targetType == 3;
        let _isNormalTarget: boolean = this.targetType == 2;
        let _newShopId: number = VendorData_blxf.instance.getNewShopId_blxf();
        let _shopId: number = -1;
        if ( _isNormalTarget && !_newShopId || true ) {
            cc.log('this.isRandomShopId >>', this.isRandomShopId);
            
            // todo 普通菜篮子一直有目标物品
            this.isRandomShopId = true;

            _shopId = this.isRandomShopId ? VendorData_blxf.instance.getVendorMaxShopId_blxf( this.buyShopId ) : -1;
            this.isRandomShopId = !this.isRandomShopId;
        } else {
            _shopId = _isVideoTarget ? -1 : VendorData_blxf.instance.getTargetShopId_blxf( true, this.buyShopId );
        }
        setTimeout(()=>{
            this.onUpdateShopId_blxf( _shopId );

            this.setTargetCompleteNum_blxf();
        }, 100)

        return true;
    }

    /**
     * 添加关卡商品数
     * @param addNum 添加数
     */
    addPassShopNum_blxf ( addNum: number ) {
        if ( !this.isPassTarget_blxf() ) return;

        let _passShopNum: number = GameData_blxf.instance.getPassShopNum_blxf();
        _passShopNum += addNum;

        if ( this.passShopInfo == -1 ) return;
        let { ShopId, NeedShopNum } = this.passShopInfo;
        let _isPass: boolean = NumModule_blxf.instance.onCompareNum_blxf( _passShopNum, NeedShopNum, true );
        UtilsModule_blxf.instance.Log_blxf([ ShopId - 1, '进阶进度 >>', _isPass, addNum, _passShopNum, NeedShopNum, { ...this.passShopInfo } ]);

        // 防止送货被覆盖
        if ( !PublicModule_blxf.isPass ) {
            PublicModule_blxf.isPass = _isPass;
            PublicModule_blxf.isPassClearExtra = _isPass;
        }

        if ( _isPass ) {
            _passShopNum = 0;

            // 检测关卡奖励
            this.onCheckPassReward_blxf();
        }
        GameData_blxf.instance.setPassShopNum_blxf( _passShopNum, _isPass );
    }

    // 检测关卡奖励
    onCheckPassReward_blxf () {
        if ( this.passShopInfo == -1 ) return;
        let { ShopId, NewGuestId, IsCommon } = this.passShopInfo;
        let _shopId = Number( ShopId );
        let _guestId = Number( NewGuestId );

        if ( _guestId ) {            
            // 获得新客人
            GameData_blxf.instance.unlockGuest_blxf( _guestId );
        }

        if ( _shopId ) {
            PublicModule_blxf.isMoveAnimation = true;

            // 获得新商品
            GameData_blxf.instance.unlockShop_blxf( _shopId );

            let _shopNum: number = GameData_blxf.instance.getShopIdList_blxf().length - 2;
            UtilsModule_blxf.onReportEvent_blxf( 2, 106, true, _shopNum );
        }

        // if ( IsCommon == 1 ) {
        //     let { RewardType, RewardNum } = this.passShopInfo;
        //     NumModule_blxf.instance.updateNumByType( Number(RewardType), Number(RewardNum) );
        //     UtilsModule_blxf.instance.Log_blxf( [ '通用关卡奖励 >>', RewardType, RewardNum ], 2 );
        // }
    }

    /** 获取关卡商品总数 */
    initPassShopInfo_blxf () {
        if ( !this.isPassTarget_blxf() ) return;

        let _shopIdList: number[] = GameData_blxf.instance.getShopIdList_blxf();
        let _passShopId: number = _shopIdList[ _shopIdList.length - 1 ];
        if ( _passShopId == ConfigModule_blxf.diamondShopId ) {
            _passShopId = _shopIdList[ _shopIdList.length - 2 ];
        }
        let { CSV_SHOP } = EnumManager_blxf.instance.getGameCsvType();
        let _cfgShopData: any[] = CsvManager_blxf.instance.getCsvDataByName_blxf( CSV_SHOP );
        _cfgShopData = _cfgShopData.filter( item => item.ShopId < 100000 && item.NeedShopNum > 0 );
        let _shopIndex: number = _cfgShopData.findIndex( item => item.ShopId == _passShopId );
        let _nextShopData: any = _cfgShopData[ _shopIndex + 1 ];

        let _passNum = GameData_blxf.instance.getPassNum_blxf();
        let _index = (_passNum - 1) % 3;
        this.passShopInfo = _nextShopData ? _nextShopData : UtilsModule_blxf.instance.onCommonShopData_blxf( _index );

        // this.passShopInfo = _nextShopData ? _nextShopData : -1;
        
        // cc.log('this.passShopNum >>', this.passShopInfo.NeedShopNum, this.passShopInfo);
    }

    /** 初始化菜篮子升级数据 */
    initTargetLvUpData_blxf () {
        // let _targetData: any = GameData_blxf.instance.getTargetData_blxf( this.targetId ) || { targetLv: 1 };
        let _targetId: number = !!this.autoTargetData ? ( this.targetId - 1 ) : [ 1, 3, 2, 4 ].indexOf( Number(this.targetId) );
        let _targetData: any = GameData_blxf.instance.getAutoTargetData_blxf( _targetId );
        let { targetLv } = _targetData || { targetLv: 1 };

        this.targetLv = targetLv;

        if ( targetLv > 6 ) targetLv = 6;
        if ( this.targetType == 1 ) targetLv = 1;
        
        let _nextTargetLv: number = targetLv + 1;

        let _nowTargetLevelData = this.getTargetLevelData_blxf( targetLv, false );
        let _nextTargetLevelData = this.getTargetLevelData_blxf( _nextTargetLv, true );

        this.onUpdateMaxShopNum_blxf( _nowTargetLevelData.ShopNum );

        this.targetLvData = _nowTargetLevelData;
        this.targetLvUpData = _nextTargetLevelData;
    }

    /** 检测商品数是否超出 */
    onCheckBuyShopNum_blxf () {
        if ( this.targetLvUpData ) return;

        let { ShopNum } = this.targetLvData;
        let _isGT: boolean = ShopNum - this.buyShopNum <= 0;
        if ( !_isGT ) return;

        this.buyShopNum = ShopNum - 10;
    }

    /**
     * 获取菜篮子等级数据
     * @param targetLv 菜篮子等级
     * @returns 等级数据
     */
    getTargetLevelData_blxf ( targetLv: number, isNextLv: boolean ) {
        let { CSV_TARGET_LEVEL } = EnumManager_blxf.instance.getGameCsvType();
        let _cfgTargetLevelList = CsvManager_blxf.instance.getCsvDataByName_blxf( CSV_TARGET_LEVEL );
        let _targetLevelData = _cfgTargetLevelList.find( item => item.TargetLv == targetLv );

        if ( !isNextLv) {
            _cfgTargetLevelList.forEach( item =>{
                let _isCheck1 = item.TargetLv - targetLv <= 0;
                if ( !_isCheck1 ) return;
    
                _targetLevelData = item;
            })
        }
        
        return _targetLevelData;
    }

    /**
     * 更新篮子商品id
     * @param shopId 商品id
     */
    onUpdateShopId_blxf ( shopId: number ) {
        if ( this.targetType == 3 && (!shopId || shopId < 0) ) {
            return UtilsModule_blxf.instance.Log_blxf('onUpdateShopId 参数错误', 2);
        }

        let _isVideoTarget: boolean = this.targetType == 1;
        /*if ( !_isVideoTarget )*/ this.buyShopId = shopId;
        this.buyShopNum = 0;

        // 保存数据
        this.saveTargetData_blxf();

        this.initBuyShopInfo_blxf();
    }

    /** 初始化需求商品信息 */
    initBuyShopInfo_blxf () {
        if ( !this.buyShopId || this.buyShopId == -1 ) return;

        let _cfgShopData = VendorData_blxf.instance.findShopData_blxf( this.buyShopId );
        if ( !_cfgShopData ) return;

        this.buyShopInfo = new BaseShopData_blxf( _cfgShopData );
    }

    /**
     * 更新最大商品数
     * @param maxShopNum 最大商品数
     */
    onUpdateMaxShopNum_blxf ( maxShopNum: number ) {
        if ( !maxShopNum || maxShopNum < 0 ) {
            return UtilsModule_blxf.instance.Log_blxf('onUpdateMaxShopNum_blxf 参数错误', 2);
        }

        this.maxShopNum = maxShopNum;
    }

    /** 获取购买商品信息 */
    getBuyShopInfo_blxf () {
        return this.buyShopInfo;
    }

    /** 获取过关商品信息 */
    getPasShopInfo_blxf () {
        return this.passShopInfo;
    }

    /** 检测菜篮子是否升级 */
    onCheckTargetLevel_blxf () {
        let _isLvUp: boolean;

        if ( !this.targetLvUpData ) return _isLvUp;

        // let { CompleteNum } = this.targetLvUpData;
        // let _completeNum: number = GameData_blxf.instance.getTargetCompleteNum_blxf( this.targetId );
        // _isLvUp = NumModule_blxf.instance.onCompareNum_blxf( _completeNum, CompleteNum );

        // // 菜篮子升级
        // _isLvUp && this.setTargetLevel_blxf();
        
        return _isLvUp;
    }

    /***** 数据存储 ******/

    /** 保存菜篮子数据 */
    saveTargetData_blxf () {
        let _targetInfo: object = { 
            isUnlock : this.isUnlock,
            buyShopId: this.buyShopId, 
            buyShopNum: this.buyShopNum,
            isRandomShopId: this.isRandomShopId
        }
        GameData_blxf.instance.setTargetInfo_blxf( this.targetId, _targetInfo );
    }

    /** 菜篮子累计达成次数 */
    setTargetCompleteNum_blxf () {
        GameData_blxf.instance.setTargetCompleteNum_blxf( this.targetId );
    }

    /** 菜篮子升级 */
    setTargetLevel_blxf () {
        // GameData_blxf.instance.setTargetLv_blxf( this.targetId );

        this.initTargetLvUpData_blxf();
    }

    /** 加载弹窗 */

}
